#include <stdio.h>
#include <SGE.h>
#include "Bullet.h"
#include "Fighter.h"
#include "Enemy.h"
using namespace SGE;

const int kMaxBullets =100; //you can also use #define MAX 100 but we are using a const int because compiler is better than preprocessor. In this case its cos if we have for loops and its for(int i=0;i<MAX;i++) but the MAX would just be 100. It has no address and MAX has no data type so its not type 

Bullet bullets[kMaxBullets];
int bulletIndex=0;

Fighter fighter;

const int kMaxEnemies = 100;
Enemy enemies[kMaxEnemies];
int enemyIndex = 0;

const int kMaxExplosions = 100;
SGE_Emitter explosions[kMaxExplosions];
int explosionIndex = 0;

SGE_Sprite background;
SGE_Sound fireSound;
SGE_Sound explosionsounds;
float spawnDelay = 0.0f;

bool quit = false;

void UpdateGameObject(float deltaTime)
{
	fighter.Update(deltaTime);

	for(int i = 0; i < kMaxBullets; ++i)
	{
	bullets[i].Update(deltaTime);
	}

	for(int i = 0; i < kMaxEnemies; ++i)
	{
	enemies[i].Update(deltaTime);
	}

	for(int i = 0; i < kMaxExplosions; ++i)
	{
	explosions[i].Update(deltaTime);
	}




}

void FireBullets()
{
	if(Input_IsKeyPressed(Keys::SPACE))
	{
		fireSound.Play();
		if (!bullets[bulletIndex].IsActive())
		{
		SVector2 pos = fighter.GetPosition();
		SVector2 vel (0.0f,-500.0f);
		bullets[bulletIndex].Fire(pos,vel);
		bulletIndex = (bulletIndex+1) % kMaxBullets;
		fireSound.Play();
			if (!bullets[bulletIndex].IsActive())
			{
			SVector2 vel2 (-100.0f,-500.0f);
			bullets[bulletIndex].Fire(pos,vel2);
			bulletIndex = (bulletIndex+1) % kMaxBullets;
			fireSound.Play();
			}

				if (!bullets[bulletIndex].IsActive())
				{
				SVector2 vel3 (-200.0f,-500.0f);
				bullets[bulletIndex].Fire(pos,vel3);
				bulletIndex = (bulletIndex+1) % kMaxBullets;
				fireSound.Play();
				}

					if (!bullets[bulletIndex].IsActive())
					{
					SVector2 vel4 (100.0f,-500.0f);
					bullets[bulletIndex].Fire(pos,vel4);
					bulletIndex = (bulletIndex+1) % kMaxBullets;
					}

	}
}
}

void SpawnEnemies(float deltaTime)
{
	spawnDelay -= deltaTime;
	if ( spawnDelay <= 0.0f)
	{
		if ( !enemies[enemyIndex].IsActive())
		{
			const int Width = IniFile_GetInt("WinWidth",800);
			const int Height =IniFile_GetInt("WinHeight",600);
			float randNumX=Width*RandomFloat(0.1f,0.9f);
			float randNumY=-100.0f;
			enemies[enemyIndex].Spawn(SVector2(randNumX,randNumY));
			enemyIndex = (enemyIndex + 1) % kMaxEnemies;
			//time to spawn
			//reset delay time
			spawnDelay = RandomFloat(0.9f,2.0f);
		}
	}
}

void CheckCollision()
{
	for (int e = 0; e< kMaxEnemies; ++e)
	{
		if (enemies[e].IsActive())
		{
			for(int b=0;b < kMaxBullets; ++b)
			{
				if(bullets[b].IsActive())
				{
					SCircle eCircle = enemies[e].GetBoundingCircle();
					SCircle bCircle = bullets[b].GetBoundingCircle();
					float xDiff = eCircle.center.x-bCircle.center.x;
					float yDiff = eCircle.center.y-bCircle.center.y;
					float dist = sqrt(xDiff*xDiff + yDiff*yDiff);
					float radii = eCircle.radius + bCircle.radius;
					if(dist<= radii)
					{
						explosionsounds.Play("explosion.wav");  
						enemies[e].Kill();
						bullets[b].Kill();

						explosions[explosionIndex].SetPosition(eCircle.center);
						explosions[explosionIndex].Start(100.0f,true);
						explosionIndex=(explosionIndex+1) % kMaxExplosions;
						break;
					}

				}
			}
		}
	}
}

void SGE_Initialize()
{

	background.Load("space.jpg");
	fighter.Load();
	const int kWidth = IniFile_GetInt("WinWidth",800);//the number is just incase the program cant get the int. Number is basically a default
	const int kHeight = IniFile_GetInt("WinHeight",600);
	SVector2 startPosition(kWidth*0.5f,kHeight*0.8f);
	fighter.SetPosition(startPosition);
	
	for(int i = 0; i < kMaxBullets; ++i)
	{
	bullets[i].Load();
	}

	for(int i = 0; i < kMaxEnemies; ++i)
	{
	enemies[i].Load();
	}

	for(int i = 0; i < kMaxExplosions; ++i)
	{
	explosions[i].Load("explosion.txt");
	}
	fireSound.Load("photongun1.wav");
	explosionsounds.Load("explosion.wav");

}

void SGE_Terminate()
{
	for(int i = 0; i < kMaxBullets; ++i)
	{
	bullets[i].Unload();
	}

	for(int i = 0; i < kMaxEnemies; ++i)
	{
	enemies[i].Unload();
	}

	fighter.Unload();

	for(int i = 0; i < kMaxExplosions; ++i)
	{
	explosions[i].Unload();
	}
	background.Unload();
	fireSound.Unload();
	explosionsounds.Unload();
}

bool SGE_Update(float deltaTime)
{
	UpdateGameObject(deltaTime);
	FireBullets();
	SpawnEnemies(deltaTime);
	CheckCollision();

	if(Input_IsKeyPressed(Keys::ESCAPE))
	{
		quit=true;
	}
	return quit;
}

void SGE_Render()
{
	background.Render();
	for(int i = 0; i < kMaxBullets; ++i)
	{
	bullets[i].Render();
	}

	for(int i = 0; i < kMaxEnemies; ++i)
	{
	enemies[i].Render();
	}

	fighter.Render();

	for(int i = 0; i < kMaxExplosions; ++i)
	{
	explosions[i].Render(true);
	}
}

//int *pt=80;
//pt++; actually is 84
//double*dptr=80
//dptr++ is 88
